package com.eleven.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eleven.pojo.Msg;
import com.eleven.pojo.News;
import com.eleven.service.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class NewsController {
	@Autowired
	NewsService newsService;

	public NewsService getNewsService() {
		return newsService;
	}

	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}
	/**
	 * 查看资讯详情By newsId
	 */
	@RequestMapping(value="/getAllNewsByNewsId/{newsId}",method=RequestMethod.GET)
	@ResponseBody
	public Msg getAllNewsByNewsId(@RequestParam(value="pn",defaultValue="1")Integer pn,@PathVariable("newsId") Integer newsId) {
		PageHelper.startPage(pn, 1);//传入页码和每页显示的数量
		List<News> news = newsService.getAllNewsByNewsId(newsId);

		PageInfo pageInfo = new PageInfo(news, 1);//1是连续显示1页的内容
		if(news.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail().add("message", "暂无私信");
		}	
	}
	/**
	 * 查看资讯详情By newsUsId
	 */
	@RequestMapping(value="/getAllNewsByNewsUserId/{newsUsId}",method=RequestMethod.GET)
	@ResponseBody
	public Msg getAllNewsByNewsUserId(@RequestParam(value="pn",defaultValue="1")Integer pn,@PathVariable("newsUsId") Integer newsUsId) {
		PageHelper.startPage(pn, 1);//传入页码和每页显示的数量
		List<News> newsList = newsService.getAllNewsByNewsUserId(newsUsId);

		PageInfo pageInfo = new PageInfo(newsList, 1);//1是连续显示1页的内容
		if(newsList.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail().add("message", "暂无私信");
		}	
	}
	/**
	 * 新增资讯
	 */
	
	/**
	 * 修改资讯
	 */
	
	/**
	 * 删除资讯
	 */

	/**
	 * 查看全部资讯
	 */
	@RequestMapping(value="/getAllNews",method=RequestMethod.GET)
	@ResponseBody
	public Msg getLetters(@RequestParam(value = "pn", defaultValue = "1") Integer pn) {
		PageHelper.startPage(pn, 5);// 引入pageHelpers
		List<News> news = newsService.getAllNews();
		PageInfo pageInfo = new PageInfo(news, 3);
		if(news.size() > 0) {
			return Msg.success().add("pageInfo", pageInfo);
		}else {
			return Msg.fail().add("message", "没有资讯信息");
		}
	}

}
